<?php

/*
 * Controller/DiplomeController.php
 */

namespace RHX\Controller;

use RHX\Model\Object\Diplome;
use RHX\Model\Table\DiplomeTable;

class DiplomeController extends RHXController {

    /**
     * Page d'accueil
     *
     * @return array
     */
    public function indexAction() {
	return array();
    }

    /**
     * Chargement du contenu de la page d'accueil
     *
     * @return array
     */
    public function loadIndexAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// table
	$diplomeTable = new DiplomeTable($this->getAdapter());

	// POST
	$post = $this->getRequest()
		->getPost();

	// Tous les diplomes
	$diplomeList = $diplomeTable->getAll();

	return array(
	    'diplomes' => $diplomeList,
	);
    }

    /**
     * Ajouter un nouveau
     *
     * @return array
     */
    public function addNewAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// table
	$diplomeTable = new DiplomeTable($this->getAdapter());

	// POST
	$post = $this->getRequest()
		->getPost();

	// Nouveau Diplome
	$diplome = new Diplome($this->getAdapter(), $post);

	// Persistance dans la base de données
	$id		 = $diplomeTable->save($diplome);
	$diplome->id	 = $id;

	return array(
	    'diplome' => $diplome,
	);
    }

    /**
     * Editer
     *
     * @return array
     */
    public function editAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// POST
	$post = $this->getRequest()
		->getPost();

	// Table
	$diplomeTable = new DiplomeTable($this->getAdapter());

	// Requête SQL
	$pk	 = $post->pk;
	$name	 = $post->name;
	$value	 = addslashes($post->value);
	$sql	 = "UPDATE rh_diplome SET $name = '$value' WHERE id = $pk";

	$result = $diplomeTable->executeQuery($sql);

	return array(
	    'sql'	 => $sql,
	    'result' => $result,
	);
    }

    /**
     * Effacer un diplome
     * 
     * @return array
     */
    public function eraseAction() {
	// Changement de layout
	$this->layout('layout/ajax');

	// POST
	$post = $this->getRequest()
		->getPost();

	// Table
	$diplomeTable = new DiplomeTable($this->getAdapter());

	$nb = $diplomeTable->erase($post->id);

	return new \Zend\View\Model\JsonModel(array(
	    'nb'	 => $nb,
	    'done'	 => ($nb > 0) ? true : false,
	));
    }

}

?>
